//
//  ReadSerialDetailTVC.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/27.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadSerialDetailTVC.h"

@implementation ReadSerialDetailTVC

- (void)dealloc {
    
    [_userImage release];
    [_serialDataModel release];
    [_userLabel release];
    [_dataLabel release];
    [_detailText release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor colorWithRed:247/255.0 green:248/255.0 blue:249/255.0 alpha:1];
        
        self.userImage = [UIImageView new];
        [self.contentView addSubview:_userImage];
        
        self.userLabel = [YHYBaseLabel new];
        self.userLabel.textColor = [UIColor blueColor];
        self.userLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.userLabel];
        
        self.dataLabel = [YHYBaseLabel new];
        self.dataLabel.textColor = [UIColor lightGrayColor];
        self.dataLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_dataLabel];
        
        self.titleLabel = [YHYBaseLabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:18];
        self.titleLabel.textColor = [UIColor grayColor];
        _titleLabel.numberOfLines = 0;
        [self.contentView addSubview:self.titleLabel];
        
        self.detailText = [YHYBaseTextView new];
        self.detailText.delegate = self;
        self.detailText.font = [UIFont systemFontOfSize:17];
        self.detailText.textColor = [UIColor grayColor];
        self.detailText.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
        self.detailText.layer.cornerRadius = 5;
        self.detailText.layer.masksToBounds = YES;
        self.detailText.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.detailText.layer.borderWidth = 2;
        self.detailText.scrollEnabled = YES;//是否可以拖动
        self.detailText.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
        [self.contentView addSubview:self.detailText];
        
        self.introduceLabel = [YHYBaseLabel new];
        self.introduceLabel.font = [UIFont systemFontOfSize:15];
        self.introduceLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.introduceLabel];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [_userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.width.equalTo(@45);
        make.height.equalTo(@45);
        
    }];
    _userImage.layer.cornerRadius = 45 / 2;
    _userImage.layer.masksToBounds = YES;
    
    [_userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(_userImage.mas_right).offset(10);
        make.height.equalTo(@(20));
        make.width.equalTo(@100);
        
    }];
    
    [_dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_userLabel.mas_bottom).offset(5);
        make.left.equalTo(_userImage.mas_right).offset(10);
        make.width.equalTo(@(150));
        make.height.equalTo(@(20));
        
    }];
    
    _titleHeight = [[self class]heightWithTitle:self.titleLabel.text];
    [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_userImage.mas_bottom).offset(15);
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-10);
        
    }];
    
    
    [_detailText mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_titleLabel).offset(40);
        make.left.equalTo(self.contentView).offset(10);
        make.width.equalTo(@(INWIDTH - 20));
        make.height.equalTo(@(INHEIGHT * 0.7));
        
    }];
    
    [_introduceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_detailText.mas_bottom).offset(20);
        make.left.equalTo(self.contentView).offset(50);
        make.height.equalTo(@20);
        make.width.equalTo(@360);
        
    }];
    
    [self layoutIfNeeded];
    
}

- (void)setSerialDataModel:(DetailSerialDataModel *)serialDataModel {
    if (_serialDataModel != serialDataModel) {
        [_serialDataModel release];
        _serialDataModel = [serialDataModel retain];
    }
    
    NSString *str = [serialDataModel.maketime substringToIndex:10];
    _dataLabel.text = str;
    _titleLabel.text = serialDataModel.title;
    
    self.detailText.text = [serialDataModel.content stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
    
    _introduceLabel.text = serialDataModel.charge_edt;
    
}


- (void)setAutoModel:(AutoModel *)autoModel {
    if (_autoModel != autoModel) {
        [_autoModel release];
        [_autoModel retain];
    }
    [_userImage sd_setImageWithURL:[NSURL URLWithString:autoModel.web_url]];
    _userLabel.text = autoModel.user_name;
}


+ (CGFloat)heightWithTitle:(NSString *)text {
    if (text == nil) {
        return 0;
    }
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return size.height + 50;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
